package cn.icanci.ladybird.common.result;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.stereotype.Component;

import java.io.Serializable;

/**
 * @author: icanci
 * @date: Created in 2020/9/27 20:20
 * @classAction: 返回统一的JSON格式数据
 */
@SuppressWarnings("all")
@Component
@Getter
@Setter
@ToString
public class JsonData implements Serializable {
    /**
     * 状态码 0 成功 1 表示处理中 -1 表示失败
     */
    private Integer code;

    /**
     * 状态描述信息
     */
    private String msg;

    /**
     * 业务数据
     */
    private Object data;


    public JsonData() {
    }


    public JsonData(Integer code, String msg) {
        this.code = code;
        this.data = null;
        this.msg = msg;
    }

    public JsonData(Integer code, Object data, String msg) {
        this.code = code;
        this.data = data;
        this.msg = msg;
    }

    /**
     * 成功不用返回数据
     *
     * @return 返回json
     */
    public static JsonData builderSuccess() {
        return new JsonData(0, null, null);
    }

    /**
     * 成功不用返回数据
     *
     * @return 返回json
     * @date 返回的数据
     */
    public static JsonData builderSuccess(Object data) {
        return new JsonData(0, data, null);
    }

    /**
     * 成功不用返回数据
     *
     * @return 返回json
     * @date 返回的数据
     */
    public static JsonData builderSuccess(Object data,String msg) {
        return new JsonData(0, data, msg);
    }


    /**
     * 失败
     *
     * @return 返回json
     */
    public static JsonData builderError(String msg) {
        return new JsonData(-1, null, msg);
    }

    /**
     * 失败
     *
     * @return 返回json
     */
    public static JsonData builderError(Integer code, String msg) {
        return new JsonData(code, null, msg);
    }
}
